<template>
  <div style="margin: auto; width: 90%; margin-top: 3%">
    <el-row>
      <el-col :span="18"
        ><el-input
          clearable
          placeholder="请输入菜品名称"
          prefix-icon="el-icon-search"
          v-model="input"
          style="width: 20%"
          @keyup.enter.native="init"
        ></el-input
      ></el-col>
      <el-col :span="6">
        <el-popconfirm
          title="确认删除该菜品, 是否继续?"
          @onConfirm="allDeleteDish(multipleSelection)"
        >
          <el-link slot="reference" type="danger">批量删除</el-link>
        </el-popconfirm>
        <el-divider direction="vertical"></el-divider>
        <el-popconfirm
          title="确认更改该菜品状态?"
          @onConfirm="allChangeDishStatus(0)"
        >
          <el-link slot="reference" type="primary">批量启售</el-link>
        </el-popconfirm>
        <el-divider direction="vertical"></el-divider>
        <el-popconfirm
          title="确认更改该菜品状态?"
          @onConfirm="allChangeDishStatus(1)"
        >
          <el-link slot="reference" type="danger">批量停售</el-link>
        </el-popconfirm>
        <el-divider direction="vertical"></el-divider>
        <el-button type="warning" @click="addDish">新建菜品</el-button>
      </el-col>
    </el-row>

    <el-table
      border:true
      :data="tableData"
      style="width: 100%"
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column prop="name" label="菜品名称" />
      <el-table-column label="图片">
        <template slot-scope="scope">
          <el-image
            :src="`${env}/common/download?name=${scope.row.image}`"
            :preview-src-list="[
              `${env}/common/download?name=${scope.row.image}`,
            ]"
            style="width: 40%"
            fit="contain "
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="菜品分类" width="250" />
      <el-table-column label="售价">
        <template slot-scope="scope"> ￥{{ scope.row.price }} </template>
      </el-table-column>
      <el-table-column label="售卖状态">
        <template slot-scope="scope">
          {{ scope.row.status == 1 ? "启售" : "停售" }}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="操作时间" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary" @click="updateDish(srow)">修改</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认更改该菜品状态?"
            @onConfirm="changeDishStatus(row)"
          >
            <el-link slot="reference" type="primary">
              {{ row.status == 1 ? "停售" : "启售" }}
            </el-link>
          </el-popconfirm>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认删除该菜品, 是否继续?"
            @onConfirm="allDeleteDish([row])"
          >
            <el-link slot="reference" type="danger">删除</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="text-align: center; margin-top: 3%"
    >
    </el-pagination>
  </div>
</template>

<script>
import {
  getDish,
  changeDishStatus0,
  changeDishStatus1,
  deleteDish,
} from "@/api/dish";
import router from "@/router";
export default {
  data() {
    return {
      env: process.env.VUE_APP_BASE_API,
      tableData: [],
      pageInfo: { page: 1, pageSize: 10 },
      total: 0,
      input: "",
      multipleSelection: [],
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getDish({ ...this.pageInfo, name: this.input }).then((res) => {
        const { records, total } = res.data;
        this.tableData = records;
        this.total = total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 修改状态
    async changeDishStatus(row) {
      let res;
      if (row.status == 1) {
        res = await changeDishStatus0({ ids: row.id });
      } else {
        res = await changeDishStatus1({ ids: row.id });
      }
      if ((res.code = 1)) {
        this.$message.success(res.data);
      } else {
        this.$message.console.error(res.data);
      }
      this.init();
    },
    // 多选修改
    async allChangeDishStatus(val) {
      let res;
      if (this.multipleSelection.length == 0) {
        this.$message.error("批量操作，请先勾选操作菜品！");
        return;
      }
      if (val == 1) {
        res = await changeDishStatus0({
          ids: this.multipleSelection.toString(),
        });
      } else {
        res = await changeDishStatus1({
          ids: this.multipleSelection.toString(),
        });
      }
      if ((res.code = 1)) {
        this.$message.success(res.data);
      } else {
        this.$message.console.error(res.data);
      }
      this.init();
    },
    // // 删除
    // async deleteDish(row) {
    //     const res = await deleteDish({ ids: row.id })
    //     if (code == 1) {
    //         this.$message.success(res.data)
    //     } else {
    //         this.$message.error(res.data)
    //     }
    //     this.init()
    // },
    // 多选删除
    async allDeleteDish(array) {
      console.log(array);

      if (array.length == 0) {
        this.$message.error("请选择删除对象");
        return;
      }
      let res = await deleteDish({
        ids: array.map((item) => item.id).toString(),
      });
      if ((res.code = 1)) {
        this.$message.success(res.data);
      } else {
        this.$message.console.error(res.data);
      }
      this.init();
    },
    // 新建菜品
    addDish() {
      router.push("/backend/dish/add");
    },
    // 修改菜品
    updateDish(row) {
      router.push("/backend/dish/update");
    },
  },
};
</script>